package com.irononetech.android.MultilevelListView;

import com.irononetech.android.Application.Application;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.irononetech.android.formcomponent.FormObject;
import com.irononetech.android.formcomponent.view.VehicleDetailsActivity;
import com.irononetech.android.claimsonersa.R;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ExpList3_HandTractor extends ExpandableListActivity{

	Logger LOG = LoggerFactory.getLogger(ExpList3_HandTractor.class);

	private ColorExpListAdapterHandTractor colorExpListAdapter;
	FormObject fo;  
	static boolean booleanlistHandTractor[][][][];
	static boolean booleanprelistHandTractor[][][][];
	Button back;
	EditText otherfield;
	
	static final String listdesc[][][][] = 
		{
		{
			{
				{ "Hand Tractor", "All parts" },
				{ "Head Light","" },
				{ "Light Cowling","" },
				{ "FR Rest","" },
				{ "FR Chassis Bar","" },
				{ "Engine","" },
				{ "Bonnet","" },
				{ "Diesel Tank","" },
				{ "Radiator","" },
				{ "Fan","" },
				{ "LH Handle","" },
				{ "RH Handle","" },
				{ "Rotavator","" },
			},
		}
		};

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle){

		try {
			super.onCreate(icicle);
			LOG.debug("ENTRY onCreate");
			setContentView(R.layout.multilevel_expandable_list_main);
			fo=Application.getFormObjectInstance();
			back = (Button)findViewById(R.id.button2);
			otherfield = (EditText)findViewById(R.id.damagedItemsOtherField);

			if(fo.getisPreSelected())  //PRE
			{
				otherfield.setText(fo.getpredamagedItemsOtherField());
				booleanprelistHandTractor = fo.getBooleanprelistHandTractor();
				colorExpListAdapter = new ColorExpListAdapterHandTractor(this, getExpandableListView(), listdesc);
				setListAdapter( colorExpListAdapter );

				back.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_button_xml));
			}
			else {
				otherfield.setText(fo.getdamagedItemsOtherField());
				booleanlistHandTractor = fo.getBooleanlistHandTractor();
				colorExpListAdapter = new ColorExpListAdapterHandTractor(this, getExpandableListView(), listdesc);
				setListAdapter( colorExpListAdapter );

				back.setBackgroundDrawable(getResources().getDrawable(R.drawable.cancel_button_xml));
			}
			if(!fo.isEditable()){
				editDisable();
			}
			LOG.debug("SUCCESS onCreate");
		} catch (NotFoundException e) {
			LOG.error("onCreate:10319");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		} catch (Exception e) {
			LOG.error("onCreate:10320");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	
	public void editDisable() {
		try {
			otherfield.setFocusable(false);

			if(fo.getisPreSelected())  //PRE
			{
				back.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_button_xml));
			} else{
				back.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_button_xml));
			}
		} catch (NotFoundException e) {
			LOG.error("editDisable:10321");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		} catch (Exception e) {
			LOG.error("editDisable:10322");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public void editEnable() {
		try {
			otherfield.setFocusable(true);

			if(fo.getisPreSelected())  //PRE
			{
				back.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_button_xml));
			} else{
				back.setBackgroundDrawable(getResources().getDrawable(R.drawable.cancel_button_xml));
			}
		} catch (NotFoundException e) {
			LOG.error("editEnable:10323");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		} catch (Exception e) {
			LOG.error("editEnable:10324");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	
	public void multilevel_ok_click(View v){
		try{
			if(fo.getisPreSelected())  //PRE
			{
				fo.setBooleanprelistHandTractor(booleanprelistHandTractor);
				fo.setpredamagedItemsOtherField(otherfield.getText().toString().trim());
			}
			else{
				fo.setBooleanlistHandTractor(booleanlistHandTractor);
				fo.setdamagedItemsOtherField(otherfield.getText().toString().trim());
			}

			Intent mIntent = new Intent(ExpList3_HandTractor.this, VehicleDetailsActivity.class);
			mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(mIntent);
			finish();
		}
		catch (Exception e) {
			LOG.error("multilevel_ok_click:11325");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public void multilevel_back_button(View v){
		try {
			if (fo.isEditable()) {
				int p1 = Application.get4DArrSizeSec1();
				int p2 = Application.get4DArrSizeSec2();
				int p3 = Application.get4DArrSizeSec3();
				int p4 = Application.get4DArrSizeSec4();
				boolean[][][][] a = new boolean[p1][p2][p3][p4];
				if(!fo.getisPreSelected()) {
					//Damaged Items
					fo.setisVehicleShow(true);
					fo.setdamagedItemsOtherField("");
					fo.setpredamagedItemsOtherField("");
					fo.setBooleanlistHandTractor(a);
					fo.setBooleanprelistHandTractor(a);
				}
				else {
					fo.setBooleanprelistHandTractor(a);
					fo.setpredamagedItemsOtherField(otherfield.getText().toString().trim());
				}
			}
			finish();
		} catch (Exception e) {
			LOG.error("multilevel_back_button:11326");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}
}